package com.tripshare.sdk.weixin.converter;

import com.tripshare.sdk.weixin.api.util.XmlUtil;
import com.tripshare.sdk.weixin.format.WeixinBaseReplyMsg;

import java.lang.reflect.InvocationTargetException;
import java.util.Map;

/**
 * 为不同的消息定义不同的序列化和反序列化转换器
 */
public class ConverterFactory {

    public static Converter getReceiveConverter(String decryptedMsg) throws IllegalAccessException, InvocationTargetException, InstantiationException {
        //反序列化
        Map<String, String> msgMap = XmlUtil.xmlToObject(decryptedMsg, Map.class);
        String msgType = msgMap.get("MsgType");
        switch (msgType) {
            case "text":
                return new TextConverter();
            case "location":
                return new LocationConverter();
            case "event":
                String event = msgMap.get("Event");
                if ("scancode_push".equals(event) || "scancode_waitmsg".equals(event)) {
                    return new ScanCodeConverter();
                } else if ("location_select".equals(event)) {
                    return new LocationConverter();
                } else if ("CLICK".equals(event)) {
                    return new EventClickConverter();
                }
            case "image":
            case "voice":
            case "video":
            case "shortvideo":
            case "link":
            default:
                return null;
        }
    }

    public static Converter getReplyConverter(WeixinBaseReplyMsg replyMsg) throws IllegalAccessException, InvocationTargetException, InstantiationException {
        switch (replyMsg.getMsgType()) {
            case "text":
                return new TextConverter();
            case "image":
            case "voice":
            case "video":
            case "shortvideo":
            case "link":
            default:
                return null;
        }
    }
}
